package com.example.demo.service.impl;

import com.example.demo.common.Result;
import com.example.demo.dto.TicketGetDTO;
import com.example.demo.entity.Ticket;
import com.example.demo.mapper.TicketMapper;
import com.example.demo.service.TicketService;
import com.example.demo.vo.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TicketServiceImpl implements TicketService {
    @Autowired
    private TicketMapper ticketMapper;

    @Override
    public Result getTickets(TicketGetDTO ticketGetDTO) {
        Ticket ticket = new Ticket();
        ticket.setId(ticketGetDTO.getId());
        ticket.setCheciId(ticketGetDTO.getCheciId());
        List<Ticket> tickets = ticketMapper.selectTickets(ticket);
        ResponseData responseData = new ResponseData();
        responseData.setList(tickets);
        responseData.setTotal(tickets.size());
        return Result.success(responseData);
    }

    @Override
    public Result updateTicket(Ticket ticket) {
        int res = ticketMapper.updateTicket(ticket);
        if (res > 0) {
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    @Override
    public Result deleteTicket(Integer id) {
        int res = ticketMapper.deleteTicket(id);
        if (res > 0) {
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    @Override
    public Result addTicket(Ticket ticket) {
        int res = ticketMapper.insertTicket(ticket);
        if(res > 0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }
}
